##### #############################################################
#####
#####   Input: Clean experimental data
#####   Output: Analysis and figures for the main paper
#####

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
rm(list=ls())

library(ggplot2)
library(data.table)
library(questionr)
library(tidyverse)
library(dplyr)
library(broom)
library(patchwork)
library(ggthemes)

load("data/reshaped_data.Rdata")

scaleFUN <- function(x) sprintf("%.2f", x)

# Descriptive results: trait preferences overall  ----------------------------------------------------
stacked_data <- stacked_data %>%
  mutate(Q2_1_factor = if_else(Q2_1 == 1, "Selected", "Not selected"), 
         Q2_2_factor = if_else(Q2_2 == 1, "Selected", "Not selected"),
         Q2_3_factor = if_else(Q2_3 == 1, "Selected", "Not selected"), 
         Q2_4_factor = if_else(Q2_4 == 1, "Selected", "Not selected"), 
         Q2_5_factor = if_else(Q2_5 == 1, "Selected", "Not selected"), 
         Q2_6_factor = if_else(Q2_6 == 1, "Selected", "Not selected"), 
         Q2_7_factor = if_else(Q2_7 == 1, "Selected", "Not selected"), 
         Q2_8_factor = if_else(Q2_8 == 1, "Selected", "Not selected"),
         Q2_9_factor = if_else(Q2_9 == 1, "Selected", "Not selected"))

means_for_plot <- data.frame(
  selected=c(
  round(prop.table(wtd.table(x = stacked_data$Q2_1_factor, weights = stacked_data$GBw8))*100,2)[2],
  round(prop.table(wtd.table(x = stacked_data$Q2_2_factor, weights = stacked_data$GBw8))*100,2)[2],
  round(prop.table(wtd.table(x = stacked_data$Q2_3_factor, weights = stacked_data$GBw8))*100,2)[2],
  round(prop.table(wtd.table(x = stacked_data$Q2_4_factor, weights = stacked_data$GBw8))*100,2)[2], 
  round(prop.table(wtd.table(x = stacked_data$Q2_5_factor, weights = stacked_data$GBw8))*100,2)[2],
  round(prop.table(wtd.table(x = stacked_data$Q2_6_factor, weights = stacked_data$GBw8))*100,2)[2], 
  round(prop.table(wtd.table(x = stacked_data$Q2_7_factor, weights = stacked_data$GBw8))*100,2)[2], 
  round(prop.table(wtd.table(x = stacked_data$Q2_8_factor, weights = stacked_data$GBw8))*100,2)[2], 
  round(prop.table(wtd.table(x = stacked_data$Q2_9_factor, weights = stacked_data$GBw8))*100,2)[2]),
  trait=c(
    "Competence", 
    "Honest",
    "Benevolent", 
    "Authentic", 
    "Caring", 
    "Assertive", 
    "Risk Taking", 
    "Collaborative", 
    "Empathy"
  ))
means_for_plot$Perc <- paste0(means_for_plot$selected, "%")

p1 <- ggplot(means_for_plot, aes(y = selected, x = reorder(trait, -selected))) +
  geom_col(fill = alpha(wesanderson::wes_palette("Moonrise3")[3],0.9)) +
   theme_clean() +
  scale_y_continuous(labels=function(x) paste0(x,"%"), limits = c(0,70), breaks = seq(0, 100, by = 10)) +
  theme(axis.text=element_text(size=9),
        plot.background = element_rect(color = NA),
        plot.caption=element_text(size=9)) +
  labs(x="", y="",
       caption = c("Note: Percentage of respondents selecting each trait as most characteristic of a trustworthy politician (weighted). N = 1,508."))
ggsave(p1, file="results/1_trait_preferences.pdf", width=7.5, height=6)

# Unconditional traits: trait ~ crisis type ----------------------------------------------------
competence <- lm(Q2_1 ~ Condition -1, data = stacked_data, weight = GBw8)
honest <- lm(Q2_2 ~ Condition -1, data = stacked_data, weight = GBw8)
benevolent <- lm(Q2_3 ~ Condition -1, data = stacked_data, weight = GBw8)
authentic <- lm(Q2_4 ~ Condition -1, data = stacked_data, weight = GBw8)
caring <- lm(Q2_5 ~ Condition -1, data = stacked_data, weight = GBw8)
assertive <- lm(Q2_6 ~ Condition -1, data = stacked_data, weight = GBw8)
risk <- lm(Q2_7 ~ Condition -1, data = stacked_data, weight = GBw8)
collaborative <- lm(Q2_8 ~ Condition -1, data = stacked_data, weight = GBw8)
empathy <- lm(Q2_9 ~ Condition -1, data = stacked_data, weight = GBw8)

coefs_for_plot <- bind_rows(
  tidy(competence, conf.int = T)%>%mutate(trait="Competence"),
  tidy(honest, conf.int = T)%>%mutate(trait="Honest"),
  tidy(benevolent, conf.int = T)%>%mutate(trait="Benevolent"),
  tidy(authentic, conf.int = T)%>%mutate(trait="Authentic"),
  tidy(caring, conf.int = T)%>%mutate(trait="Caring"),
  tidy(assertive, conf.int = T)%>%mutate(trait="Assertive"),
  tidy(risk, conf.int = T)%>%mutate(trait="Risk taking"),
  tidy(collaborative, conf.int = T)%>%mutate(trait="Collaborative"),
  tidy(empathy, conf.int = T)%>%mutate(trait="Empathy")
) %>%
  mutate(
    crisis=c("Control","Economic", "Health", "Security",
             "Control","Economic", "Health", "Security",
             "Control","Economic", "Health", "Security",
             "Control","Economic", "Health", "Security",
             "Control","Economic", "Health", "Security",
             "Control","Economic", "Health", "Security",
             "Control","Economic", "Health", "Security",
             "Control","Economic", "Health", "Security",
             "Control","Economic", "Health", "Security")
  )

p2 <- ggplot(coefs_for_plot, aes(y=estimate,x=crisis)) +
  geom_point(position = position_dodge(width=0.2)) + 
  facet_wrap(~ trait) + ylab("") + xlab("") +
  geom_linerange(position = position_dodge(width=0.2), aes(ymin = conf.low, ymax = conf.high)) +
  scale_y_continuous(labels=scaleFUN) +
  theme_clean() + 
  theme(axis.text=element_text(size=8),
        plot.background = element_rect(color = NA),
        strip.text = element_text(size=8,face = "bold", hjust = 0.5), 
        legend.position="none", 
        plot.caption = element_text(size=8),
        strip.background = element_rect(fill = grey(.9)),
        axis.title=element_text(size=9)) +
  ylab("Predicted proportion of respondents selecting trait") +
  labs(caption = c("Note: Figure shows predicted proportions of respondents selecting each trait, calculated from separate weighted OLS models
                          regressing trait selection (0 = not selected, 1 = selected) on crisis type (control, economic, health, security). N = 1,508.  "))
ggsave(p2, file="results/2_traits_crisis.pdf", width=7, height=5)

# Unconditional traits: trait ~ gender ----------------------------------------------------
competence <- lm(Q2_1 ~ gender -1, data = stacked_data, weight = GBw8)
honest <- lm(Q2_2 ~ gender -1, data = stacked_data, weight = GBw8)
benevolent <- lm(Q2_3 ~ gender -1, data = stacked_data, weight = GBw8)
authentic <- lm(Q2_4 ~ gender -1, data = stacked_data, weight = GBw8)
caring <- lm(Q2_5 ~ gender -1, data = stacked_data, weight = GBw8)
assertive <- lm(Q2_6 ~ gender -1, data = stacked_data, weight = GBw8)
risk <- lm(Q2_7 ~ gender -1, data = stacked_data, weight = GBw8)
collaborative <- lm(Q2_8 ~ gender -1, data = stacked_data, weight = GBw8)
empathy <- lm(Q2_9 ~ gender -1, data = stacked_data, weight = GBw8)


coefs_for_plot <- bind_rows(
  tidy(competence, conf.int = T)%>%mutate(trait="Competence"),
  tidy(honest, conf.int = T)%>%mutate(trait="Honest"),
  tidy(benevolent, conf.int = T)%>%mutate(trait="Benevolent"),
  tidy(authentic, conf.int = T)%>%mutate(trait="Authentic"),
  tidy(caring, conf.int = T)%>%mutate(trait="Caring"),
  tidy(assertive, conf.int = T)%>%mutate(trait="Assertive"),
  tidy(risk, conf.int = T)%>%mutate(trait="Risk taking"),
  tidy(collaborative, conf.int = T)%>%mutate(trait="Collaborative"),
  tidy(empathy, conf.int = T)%>%mutate(trait="Empathy")
) %>%
  mutate(
    gender=c("Men", "Women", 
             "Men", "Women", 
             "Men", "Women", 
             "Men", "Women", 
             "Men", "Women", 
             "Men", "Women", 
             "Men", "Women", 
             "Men", "Women", 
             "Men", "Women")
  )

p3 <- ggplot(coefs_for_plot, aes(y=estimate,x=gender)) +
  geom_point(position = position_dodge(width=0.2)) + 
  facet_wrap(~ trait) + ylab("") + xlab("") +
  geom_linerange(position = position_dodge(width=0.2), aes(ymin = conf.low, ymax = conf.high)) +
  scale_y_continuous(labels=scaleFUN) +
  theme_clean() + 
  theme(axis.text=element_text(size=8),
        plot.background = element_rect(color = NA),
        strip.text = element_text(size=8,face = "bold", hjust = 0.5), 
        legend.position="none", 
        plot.caption = element_text(size=7),
        strip.background = element_rect(fill = grey(.9)),
        axis.title=element_text(size=9)) +
  ylab("Predicted proportion of respondents selecting trait") +
labs( caption = "Note: Figure shows predicted proportions of respondents selecting each trait, calculated from separate weighted OLS models regressing trait selection 
                 (0 = not selected, 1 = selected) on respondent gender (men, women). Points represent predicted proportions with 95% confidence intervals (N = 1,508).")
ggsave(p3, file="results/3_traits_gender.pdf", width=7, height=5)

# Unconditional gendered traits: fem/masc/neutral ~ crisis type  ----------------------------------------------------

feminine <- summary(lm(feminine_traits ~ Condition -1, data = stacked_data, 
   weight = GBw8))
masculine <- lm(masculine_traits ~ Condition -1, data = stacked_data, 
   weight = GBw8)
neutral <- lm(neutral_traits ~ Condition -1, data = stacked_data, 
   weight = GBw8)

coefs_for_plot <- bind_rows(
  tidy(feminine, conf.int = T)%>%mutate(trait="Feminine Traits"),
  tidy(masculine, conf.int = T)%>%mutate(trait="Masculine Traits"),
  tidy(neutral, conf.int = T)%>%mutate(trait="Neutral Traits")) %>%
  mutate(crisis_type=c("Control","Economic", "Health", "Security",
                       "Control","Economic", "Health", "Security",
                       "Control","Economic", "Health", "Security"))

p4 <- ggplot(coefs_for_plot, aes(y=estimate,x=crisis_type)) +
  geom_point(position = position_dodge(width=0.2)) + 
  facet_wrap(~ trait) + ylab("") + xlab("") +
  geom_linerange(position = position_dodge(width=0.2), aes(ymin = conf.low, ymax = conf.high)) +
  scale_y_continuous(labels=scaleFUN) +
  theme_clean() + 
  theme(axis.text=element_text(size=7),
        plot.background = element_rect(color = NA),
        strip.text = element_text(size=8, face = "bold", hjust = 0.5), 
        plot.caption = element_text(size=7),
        strip.background = element_rect(fill = grey(.9)),
        axis.title=element_text(size=7)) + 
  ylab("Predicted proportion of respondents selecting\ngendered trait type") +
  labs(
  caption = "Note: Figure shows predicted proportions of respondents selecting each gendered trait grouping (feminine, masculine, neutral), calculated
  from separate weighted OLS models regressing gendered trait type selection (0 = not selected, 1 = selected) on crisis type (control, 
   economic, health, security). Points represent predicted proportions with 95% confidence intervals (N = 1,508).                                   "
) 
ggsave(p4, file = "results/4_gendered_traits.pdf",height = 3, width = 6)

# Conditional gendered traits: fem/masc/neutral ~ crisis type * gender  ----------------------------------------------------

feminine_woman <- lm(feminine_traits ~ Condition -1, data = stacked_data, weight = GBw8, subset = stacked_data$gender=="Woman")
feminine_man <- lm(feminine_traits ~ Condition -1, data = stacked_data, weight = GBw8, subset = stacked_data$gender=="Man")

masculine_woman <- lm(masculine_traits ~ Condition -1, data = stacked_data, weight = GBw8, subset = stacked_data$gender=="Woman")
masculine_man <- lm(masculine_traits ~ Condition -1, data = stacked_data, weight = GBw8, subset = stacked_data$gender=="Man")

neutral_woman <- lm(neutral_traits ~ Condition -1, data = stacked_data,  weight = GBw8, subset = stacked_data$gender=="Woman")
neutral_man <- lm(neutral_traits ~ Condition -1, data = stacked_data, weight = GBw8, subset = stacked_data$gender=="Man")

coefs_for_plot <- bind_rows(
  tidy(feminine_woman, conf.int = T)%>%mutate(trait="Feminine leadership traits", gender="Woman"),
  tidy(feminine_man, conf.int = T)%>%mutate(trait="Feminine leadership traits",  gender="Man"),
  tidy(masculine_woman, conf.int = T)%>%mutate(trait="Masculine leadership traits", gender="Woman"),
  tidy(masculine_man, conf.int = T)%>%mutate(trait="Masculine leadership traits", gender="Man"),
  tidy(neutral_woman, conf.int = T)%>%mutate(trait="Neutral leadership traits", gender="Woman"), 
  tidy(neutral_man, conf.int = T)%>%mutate(trait="Neutral leadership traits", gender="Man")                                            ) %>%
  mutate(crisis_type=c("Control", "Economic", "Health", "Security", 
                  "Control", "Economic", "Health", "Security", 
                  "Control", "Economic", "Health", "Security", 
                  "Control", "Economic", "Health", "Security", 
                  "Control", "Economic", "Health", "Security", 
                  "Control", "Economic", "Health", "Security")
         )

p5 <- ggplot(coefs_for_plot, aes(y=estimate,x=crisis_type, color=gender, shape = gender)) +
  geom_point(position = position_dodge(width=0.3)) + 
  facet_wrap(~ trait, nrow=2) + ylab("") + xlab("") +
  geom_linerange(position = position_dodge(width=0.3), aes(ymin = conf.low, ymax = conf.high)) +
  scale_y_continuous(labels=scaleFUN) +
  theme_clean() + 
    theme(axis.text=element_text(size=8),
        plot.background = element_rect(color = NA),
         strip.text = element_text(size=8, face = "bold", hjust = 0), 
         strip.background = element_rect(fill = grey(.9)),
        legend.text = element_text(size=8),
        legend.position = "bottom", 
        axis.title=element_text(size=8),
        legend.background = element_blank(),          
        legend.box.background = element_blank(), 
        legend.title=element_blank(), 
        plot.caption = element_text(size=7),
        legend.margin=margin(c(-15,0,0,5))) +
   scale_color_manual(values = wesanderson::wes_palette("Moonrise1")[c(4,2)], labels = c("Men", "Women"), 
                      guide = guide_legend(reverse = TRUE)) +
  scale_shape_manual(values = c(1,19), labels = c("Men", "Women"), 
                        guide = guide_legend(reverse = TRUE)) +
    ylab("Predicted proportion of respondents selecting\ngendered trait type") +
  labs(color="", shape="", 
       caption = c(
       "Note: Figure shows predicted proportions of respondents selecting each gendered trait grouping (feminine, masculine, neutral),
       calculated from separate weighted OLS models regressing gendered trait selection (0=not selected, 1=selected) on crisis
       type (control, economic, health, security) run separately for men and women. Points represent predicted proportions with 
       95% confidence intervals for men in black and women in green. N = 1,508.                                                                          ")) 
ggsave(p5, file = "results/5_gendered_traits_crisis.pdf", height = 5, width = 5.6)

